//
//  PainterLine.m
//  ChartRefactor
//
//  Created by CaoJun on 15/3/16.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterLine.h"

using namespace std;
using namespace ytx;

@implementation PainterLine

- (void)drawRect:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    Quotation * data = indexPosition->getData();

    vector<vector<double> > lineData = data->lineData;
    vector<string> lineType = data->titleType;
    int i= 0;

    for(i = 0 ; i< lineData.size() ; i++)
    {
        NSString *color = @"#2b2f33";
        if (i*2 < lineType.size()) {
            if (data->type == QuotationType::AVG) {
                color = [NSString stringWithUTF8String:lineType[i==0?2:0].c_str()];
            } else if ((data->type == QuotationType::Volume || data->type == QuotationType::Tuov) && (data->quotationType == ytx::LineType::L1d || data->quotationType == ytx::LineType::L1w || data->quotationType == ytx::LineType::L5m || data->quotationType == ytx::LineType::L15m || data->quotationType == ytx::LineType::L30m || data->quotationType == ytx::LineType::L60m)) {
                color = [NSString stringWithUTF8String:lineType[i*2 + 2].c_str()];
            }
            else {
                color = [NSString stringWithUTF8String:lineType[i*2].c_str()];
            }
        }
        else {
//            color = [NSString stringWithUTF8String:(*lineType.end()).c_str()];
        }
//        this->drawLine(lineData[i], lineType[i*2]);
        [self drawLine:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withLineData:lineData[i]  withColor:color];
    }

}

- (void)drawLine:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition withLineData:(vector<double>) lineData withColor:(NSString *)color
{
    [ctx save];
    Quotation * data = indexPosition->getData();

    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();

    float beginX = rect.getBeginX();
    [ctx setStrokeStyle:color ];
    [ctx setLineWidth:style.quoteLineWidth];
    [ctx setLineCap: kCGLineCapRound];
    [ctx setLineJoin: kCGLineJoinRound];

    if(data->lineDataDrawPartIndex.size()){
        const std::vector<pair<int,int> > & partIndex = data->lineDataDrawPartIndex;

        for (std::vector<pair<int,int> >::const_iterator it = partIndex.begin(); it != partIndex.end(); ++it) {
            pair<int,int> index = (*it);
//            beginX = drawLine(beginX, lineData.begin()+index.first, lineData.begin()+ index.second);
            beginX = [self drawLine:cgrect withContext:ctx withViewModelIndexPosition: indexPosition withBeginX:beginX withIteratiorFirst:lineData.begin()+index.first withIteratiorLast:lineData.begin()+ index.second];
        }

    }
    else{
        int startIndex = indexPosition->getStartDataIndex();
        int endIndex = indexPosition->getEndDataIndex();
//        drawLine(beginX, lineData.begin()+startIndex, lineData.begin()+ endIndex);
        [self drawLine:cgrect withContext:ctx withViewModelIndexPosition: indexPosition withBeginX:beginX withIteratiorFirst:lineData.begin()+startIndex withIteratiorLast:lineData.begin()+ endIndex];
    }

}

- (CGFloat)drawLine:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition withBeginX:(CGFloat) beginX withIteratiorFirst:(vector<double>::const_iterator) first withIteratiorLast:(vector<double>::const_iterator) last
{
    Rectangle & rect = indexPosition->getRectangle();

    float zoomX = rect.getZoomX();
    float tempX = beginX;
    int tempY = 0;
    double value;
    NSMutableArray * points = [NSMutableArray new];
    for (vector<double>::const_iterator it = first; it != last; ++it) {
        value = (*it);
        tempX += zoomX;
        if(!isnan(value)){
            tempY = rect.getPxY(value);
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(tempX, tempY)]];
//            points.push_back(ccp(tempX,tempY));
        }
    }

    if(points.count == 0){
        return beginX;
    }

    if (points.count > 0 && [points[0] CGPointValue].x != beginX) {
//        points.insert(points.begin(), ccp(beginX, points[0].y));
        [points insertObject:[NSValue valueWithCGPoint:CGPointMake(beginX, [points[0] CGPointValue].y)] atIndex:0];
    }

//    ctx.drawLines(points);
    [ctx drawLines: [NSArray arrayWithArray:points] ];

    return tempX;
}


@end
